-- -------------------------------------------------------------------------
-- Features/Vehicle.lua
-- Vehicle Customization with VehicleCustomizer
-- -------------------------------------------------------------------------

local module = {}

-- [Lua 5.4] <const> for function references
local pcall <const> = pcall

-- [Dependencies]
local ui = nil
local natives = nil
local lang = nil
local vehicle_customizer = nil
local debugger = nil

-- [Natives Cache]
local N = {}

local function _T(text)
    if _G and _G._T then
        return _G._T(text)
    end
    if lang then
        if lang.get then return lang.get(text) end
        if lang.get_text then return lang.get_text(text) end
    end
    return text
end

local function translate_list(list)
    local options = {}
    for i, value in ipairs(list) do
        options[i] = _T(value)
    end
    return options
end

-- -------------------------------------------------------------------------
-- Module State
-- -------------------------------------------------------------------------

local state = {
    main_tab = 1,
    color_mode = 1,
    primary_color = 1,
    secondary_color = 1,
    pearlescent_id = 0,
    wheel_color_id = 0,
    chameleon_paint = 1,
    xenon_enabled = false,
    xenon_color = 1,
    neon_enabled = false,
    neon_color = 1,
    tire_smoke_enabled = false,
    tire_smoke_color = 1,
    wheel_type = 1,
    wheel_index = 0,
    custom_tires = false,
    window_tint = 1,
    plate_type = 1,
    plate_text = "",
    theme_index = 1,
    interior_color_id = 0,
    dashboard_color_id = 0,
    mods = {},
    extras = {},
    doors_open = {
        [0] = false, [1] = false, [2] = false,
        [3] = false, [4] = false, [5] = false,
    },
    windows_down = {
        [0] = false, [1] = false,
        [2] = false, [3] = false,
    },
    convertible_roof = true,
    engine_on = true,
    lights_on = false,
    hazard_lights = false,
}

local cached_options = {
    paint_names = nil,
    xenon_names = nil,
    neon_names = nil,
    smoke_names = nil,
    wheel_type_names = nil,
    theme_names = nil,
    chameleon_names = nil,
    plate_type_names = nil,
    tint_names = nil,
}

local MAIN_TABS = { _T("Customization"), _T("Control") }
local COLOR_MODES = { _T("RGB Color"), _T("Chameleon Paint") }

local VISUAL_MOD_CATEGORIES = {
    {
        name = _T("Exterior Parts"),
        slots = {
            { slot = 0,  name = _T("Spoiler") },
            { slot = 1,  name = _T("Front Bumper") },
            { slot = 2,  name = _T("Rear Bumper") },
            { slot = 3,  name = _T("Side Skirt") },
            { slot = 4,  name = _T("Exhaust") },
            { slot = 5,  name = _T("Chassis") },
            { slot = 6,  name = _T("Grille") },
            { slot = 7,  name = _T("Hood") },
            { slot = 8,  name = _T("Left Fender") },
            { slot = 9,  name = _T("Right Fender") },
            { slot = 10, name = _T("Roof") },
        }
    },
    {
        name = _T("Interior"),
        slots = {
            { slot = 27, name = _T("Trim") },
            { slot = 28, name = _T("Ornaments") },
            { slot = 29, name = _T("Dashboard") },
            { slot = 30, name = _T("Dial") },
            { slot = 31, name = _T("Door Speaker") },
            { slot = 32, name = _T("Seats") },
            { slot = 33, name = _T("Steering Wheel") },
            { slot = 34, name = _T("Shifter") },
            { slot = 35, name = _T("Plaques") },
            { slot = 36, name = _T("Speakers") },
            { slot = 37, name = _T("Trunk") },
        }
    },
    {
        name = _T("Horn"),
        slots = {
            { slot = 14, name = _T("Horn") },
        }
    },
    {
        name = _T("Other Parts"),
        slots = {
            { slot = 38, name = _T("Hydraulics 2") },
            { slot = 39, name = _T("Engine Block") },
            { slot = 40, name = _T("Air Filter") },
            { slot = 41, name = _T("Struts") },
            { slot = 42, name = _T("Arch Cover") },
            { slot = 43, name = _T("Aerials") },
            { slot = 44, name = _T("Trim 2") },
            { slot = 45, name = _T("Tank") },
            { slot = 46, name = _T("Left Door") },
            { slot = 47, name = _T("Right Door") },
            { slot = 48, name = _T("Livery") },
            { slot = 49, name = _T("Plate Holder") },
        }
    },
}

-- -------------------------------------------------------------------------
-- Initialization
-- -------------------------------------------------------------------------

function module.init(ui_lib, _, nat_lib, lang_lib, _, _, debug_lib)
    ui = ui_lib
    natives = nat_lib
    lang = lang_lib
    debugger = debug_lib

    -- Cache natives
    if natives then
        N.PLAYER_PED_ID = natives.PLAYER_PED_ID
        N.IS_PED_IN_ANY_VEHICLE = natives.IS_PED_IN_ANY_VEHICLE
        N.GET_VEHICLE_PED_IS_IN = natives.GET_VEHICLE_PED_IS_IN
        N.SET_VEHICLE_FIXED = natives.SET_VEHICLE_FIXED
    end

    local vc_success, vc = pcall(require, "ClickUl.Utils.VehicleCustomizer")
    if vc_success and vc then
        vehicle_customizer = vc
        local init_success, init_err = pcall(function()
            vehicle_customizer.init(natives, debugger)
        end)
        if init_success then
            if debugger then debugger.info("[Vehicle]", "VehicleCustomizer loaded and initialized") end
            cached_options.paint_names = vehicle_customizer.build_dropdown_options(vehicle_customizer.PAINT_PRESETS, _T)
            cached_options.xenon_names = vehicle_customizer.build_dropdown_options(vehicle_customizer.XENON_PRESETS, _T)
            cached_options.neon_names = vehicle_customizer.build_dropdown_options(vehicle_customizer.NEON_PRESETS, _T)
            cached_options.smoke_names = vehicle_customizer.build_dropdown_options(vehicle_customizer.SMOKE_PRESETS, _T)
            cached_options.wheel_type_names = vehicle_customizer.build_dropdown_options(vehicle_customizer.WHEEL_TYPES, _T)
            cached_options.theme_names = vehicle_customizer.build_dropdown_options(vehicle_customizer.THEME_PACKS, _T)
            cached_options.chameleon_names = vehicle_customizer.build_dropdown_options(vehicle_customizer.CHAMELEON_PRESETS, _T)
            cached_options.plate_type_names = translate_list(vehicle_customizer.PLATE_TYPES)
            cached_options.tint_names = translate_list(vehicle_customizer.WINDOW_TINTS)
        else
            print("[Vehicle] WARNING: VehicleCustomizer init failed: " .. tostring(init_err))
            vehicle_customizer = nil
        end
    else
        print("[Vehicle] WARNING: VehicleCustomizer not available: " .. tostring(vc))
    end

    return true
end

-- -------------------------------------------------------------------------
-- Helper Functions
-- -------------------------------------------------------------------------

local function get_current_vehicle()
    if not N.PLAYER_PED_ID then return nil end

    local ped = N.PLAYER_PED_ID()
    if not ped or ped == 0 then return nil end

    local in_vehicle = N.IS_PED_IN_ANY_VEHICLE and N.IS_PED_IN_ANY_VEHICLE(ped, false)
    if not in_vehicle then return nil end

    local vehicle = N.GET_VEHICLE_PED_IS_IN and N.GET_VEHICLE_PED_IS_IN(ped, false)
    if not vehicle or vehicle == 0 then return nil end

    return vehicle
end

local function notify_no_vehicle()
    if ui and ui.notify then
        ui.notify(_T("Please enter a vehicle first"), "Warning")
    end
end

-- -------------------------------------------------------------------------
-- UI: Color Section
-- -------------------------------------------------------------------------

local function draw_color_section(vehicle)
    ui.Group(_T("Color"), function()
        local new_mode = ui.Combo(_T("Color Mode"), state.color_mode, COLOR_MODES)
        if new_mode ~= state.color_mode then
            state.color_mode = new_mode
        end

        if state.color_mode == 1 then
            ui.Row({
                function()
                    local new_primary = ui.Combo(_T("Primary"), state.primary_color, cached_options.paint_names)
                    if new_primary ~= state.primary_color then
                        state.primary_color = new_primary
                        if vehicle then
                            local preset = vehicle_customizer.PAINT_PRESETS[state.primary_color]
                            if preset and preset.value then
                                vehicle_customizer.set_custom_primary_color(vehicle, preset.value.r, preset.value.g, preset.value.b)
                            end
                        end
                    end
                end,
                function()
                    local new_secondary = ui.Combo(_T("Secondary"), state.secondary_color, cached_options.paint_names)
                    if new_secondary ~= state.secondary_color then
                        state.secondary_color = new_secondary
                        if vehicle then
                            local preset = vehicle_customizer.PAINT_PRESETS[state.secondary_color]
                            if preset and preset.value then
                                vehicle_customizer.set_custom_secondary_color(vehicle, preset.value.r, preset.value.g, preset.value.b)
                            end
                        end
                    end
                end,
            })
        else
            local new_chameleon = ui.Combo(_T("Chameleon Paint"), state.chameleon_paint, cached_options.chameleon_names)
            if new_chameleon ~= state.chameleon_paint then
                state.chameleon_paint = new_chameleon
                if vehicle then
                    local preset = vehicle_customizer.CHAMELEON_PRESETS[state.chameleon_paint]
                    if preset and preset.id >= 161 then
                        vehicle_customizer.set_vehicle_color(vehicle, preset.id, preset.id)
                    end
                end
            end
        end

        ui.Row({
            function()
                local new_pearl = ui.Slider(_T("Pearlescent"), state.pearlescent_id, 0, 159, 1)
                if new_pearl ~= state.pearlescent_id then
                    state.pearlescent_id = new_pearl
                    if vehicle then
                        vehicle_customizer.set_pearlescent_color(vehicle, state.pearlescent_id)
                    end
                end
            end,
            function()
                local new_wheel_color = ui.Slider(_T("Wheel Color"), state.wheel_color_id, 0, 159, 1)
                if new_wheel_color ~= state.wheel_color_id then
                    state.wheel_color_id = new_wheel_color
                    if vehicle then
                        vehicle_customizer.set_wheel_color(vehicle, state.wheel_color_id)
                    end
                end
            end,
        })
    end)
end

-- -------------------------------------------------------------------------
-- UI: Lights Section
-- -------------------------------------------------------------------------

local function draw_lights_section(vehicle)
    ui.Group(_T("Lights"), function()
        ui.Row({
            function()
                local new_xenon_color = ui.Combo(_T("Xenon Headlights"), state.xenon_color, cached_options.xenon_names)
                if new_xenon_color ~= state.xenon_color then
                    state.xenon_color = new_xenon_color
                    if vehicle then
                        local preset = vehicle_customizer.XENON_PRESETS[state.xenon_color]
                        if preset then
                            if preset.id == -1 then
                                vehicle_customizer.set_xenon_enabled(vehicle, false)
                            else
                                vehicle_customizer.set_xenon_color(vehicle, preset.id)
                            end
                        end
                    end
                end
            end,
            function()
                local new_neon_color = ui.Combo(_T("Neon Lights"), state.neon_color, cached_options.neon_names)
                if new_neon_color ~= state.neon_color then
                    state.neon_color = new_neon_color
                    if vehicle then
                        local preset = vehicle_customizer.NEON_PRESETS[state.neon_color]
                        if preset and preset.value then
                            vehicle_customizer.set_neon_color(vehicle, preset.value.r, preset.value.g, preset.value.b)
                            vehicle_customizer.set_all_neons_enabled(vehicle, true)
                        else
                            vehicle_customizer.set_all_neons_enabled(vehicle, false)
                        end
                    end
                end
            end,
        })
    end)
end

-- -------------------------------------------------------------------------
-- UI: Wheels Section
-- -------------------------------------------------------------------------

local function draw_wheels_section(vehicle)
    ui.Group(_T("Wheels"), function()
        local new_wheel_type = ui.Combo(_T("Wheel Type"), state.wheel_type, cached_options.wheel_type_names)
        if new_wheel_type ~= state.wheel_type then
            state.wheel_type = new_wheel_type
            state.wheel_index = 0
            if vehicle then
                local preset = vehicle_customizer.WHEEL_TYPES[state.wheel_type]
                if preset then
                    vehicle_customizer.set_wheel_type(vehicle, preset.id)
                end
            end
        end

        local max_wheels = 0
        if vehicle then
            max_wheels = vehicle_customizer.get_num_mods(vehicle, 23)
        end

        local wheel_label = _T("Wheel Style")
        if max_wheels > 0 then
            wheel_label = _T("Wheel Style") .. " (0-" .. (max_wheels - 1) .. ")"
        end
        local new_wheel_index = ui.Slider(wheel_label, state.wheel_index, 0, math.max(0, max_wheels - 1), 1)
        if new_wheel_index ~= state.wheel_index then
            state.wheel_index = new_wheel_index
            if vehicle then
                vehicle_customizer.set_front_wheels(vehicle, state.wheel_index, state.custom_tires)
            end
        end

        ui.Row({
            function()
                local new_custom_tires = ui.Checkbox(_T("Custom Tires"), state.custom_tires)
                if new_custom_tires ~= state.custom_tires then
                    state.custom_tires = new_custom_tires
                    if vehicle then
                        vehicle_customizer.set_front_wheels(vehicle, state.wheel_index, state.custom_tires)
                    end
                end
            end,
            function()
                local new_smoke_color = ui.Combo(_T("Tire Smoke"), state.tire_smoke_color, cached_options.smoke_names)
                if new_smoke_color ~= state.tire_smoke_color then
                    state.tire_smoke_color = new_smoke_color
                    if vehicle then
                        local preset = vehicle_customizer.SMOKE_PRESETS[state.tire_smoke_color]
                        if preset and preset.value then
                            vehicle_customizer.set_tire_smoke_color(vehicle, preset.value.r, preset.value.g, preset.value.b)
                        else
                            vehicle_customizer.set_tire_smoke_enabled(vehicle, false)
                        end
                    end
                end
            end,
        })
    end)
end

-- -------------------------------------------------------------------------
-- UI: Plate Section
-- -------------------------------------------------------------------------

local function draw_plate_section(vehicle)
    ui.Group(_T("License Plate"), function()
        local plate_type_names = cached_options.plate_type_names or vehicle_customizer.PLATE_TYPES
        local new_plate_type = ui.Combo(_T("Plate Style"), state.plate_type, plate_type_names)
        if new_plate_type ~= state.plate_type then
            state.plate_type = new_plate_type
            if vehicle then
                vehicle_customizer.set_plate_type(vehicle, state.plate_type - 1)
            end
        end

        local new_plate_text = ui.TextInput(_T("Plate Text"), state.plate_text, 8)
        if new_plate_text ~= state.plate_text then
            state.plate_text = new_plate_text
            if vehicle and state.plate_text ~= "" then
                vehicle_customizer.set_plate_text(vehicle, state.plate_text)
            end
        end
    end)
end

-- -------------------------------------------------------------------------
-- UI: Window Section
-- -------------------------------------------------------------------------

local function draw_window_section(vehicle)
    ui.Group(_T("Windows"), function()
        local tint_names = cached_options.tint_names or vehicle_customizer.WINDOW_TINTS
        local new_tint = ui.Combo(_T("Window Tint"), state.window_tint, tint_names)
        if new_tint ~= state.window_tint then
            state.window_tint = new_tint
            if vehicle then
                vehicle_customizer.set_window_tint(vehicle, state.window_tint - 1)
            end
        end
    end)
end

-- -------------------------------------------------------------------------
-- UI: Interior Section
-- -------------------------------------------------------------------------

local function draw_interior_section(vehicle)
    local supports_interior = false
    if vehicle then
        local interior_slots = { 32, 33, 29, 30 }
        for _, slot in ipairs(interior_slots) do
            if vehicle_customizer.get_num_mods(vehicle, slot) > 0 then
                supports_interior = true
                break
            end
        end
    end

    if not supports_interior then
        return
    end

    ui.Group(_T("Interior"), function()
        local new_interior = ui.Slider(_T("Interior Color"), state.interior_color_id, 0, 159, 1)
        if new_interior ~= state.interior_color_id then
            state.interior_color_id = new_interior
            if vehicle then
                vehicle_customizer.set_interior_color(vehicle, state.interior_color_id)
            end
        end

        local new_dashboard = ui.Slider(_T("Dashboard Color"), state.dashboard_color_id, 0, 159, 1)
        if new_dashboard ~= state.dashboard_color_id then
            state.dashboard_color_id = new_dashboard
            if vehicle then
                vehicle_customizer.set_dashboard_color(vehicle, state.dashboard_color_id)
            end
        end
    end)
end

-- -------------------------------------------------------------------------
-- UI: Visual Mod Category
-- -------------------------------------------------------------------------

local function draw_visual_mod_category(vehicle, category)
    local available_count = 0
    local available_mods = {}
    if vehicle then
        for _, mod_info in ipairs(category.slots) do
            local max_mods = vehicle_customizer.get_num_mods(vehicle, mod_info.slot)
            if max_mods > 0 then
                available_count = available_count + 1
                available_mods[#available_mods + 1] = { info = mod_info, max = max_mods }
            end
        end
    end

    -- Only show if vehicle is present and has available mods
    if available_count > 0 then
        local header = category.name .. " (" .. available_count .. ")"

        ui.Group(header, function()

            for i = 1, #available_mods, 2 do
                local mod1 = available_mods[i]
                local mod2 = available_mods[i + 1]

                if mod2 then
                    ui.Row({
                        function()
                            local slot = mod1.info.slot
                            local current = state.mods[slot] or -1
                            local label = mod1.info.name .. " (-1=" .. _T("Stock") .. ")"
                            local new_val = ui.Slider(label, current, -1, mod1.max - 1, 1)
                            if new_val ~= current then
                                state.mods[slot] = new_val
                                vehicle_customizer.set_mod(vehicle, slot, new_val, false)
                            end
                        end,
                        function()
                            local slot = mod2.info.slot
                            local current = state.mods[slot] or -1
                            local label = mod2.info.name .. " (-1=" .. _T("Stock") .. ")"
                            local new_val = ui.Slider(label, current, -1, mod2.max - 1, 1)
                            if new_val ~= current then
                                state.mods[slot] = new_val
                                vehicle_customizer.set_mod(vehicle, slot, new_val, false)
                            end
                        end,
                    })
                else
                    local slot = mod1.info.slot
                    local current = state.mods[slot] or -1
                    local label = mod1.info.name .. " (-1=" .. _T("Stock") .. ")"
                    local new_val = ui.Slider(label, current, -1, mod1.max - 1, 1)
                    if new_val ~= current then
                        state.mods[slot] = new_val
                        vehicle_customizer.set_mod(vehicle, slot, new_val, false)
                    end
                end
            end
        end)
    end
end

-- -------------------------------------------------------------------------
-- UI: Theme Section
-- -------------------------------------------------------------------------

local function draw_theme_section(vehicle)
    ui.Group(_T("Theme Pack"), function()
        local new_theme = ui.Combo(_T("Select Theme"), state.theme_index, cached_options.theme_names)
        if new_theme ~= state.theme_index then
            state.theme_index = new_theme
        end

        if ui.Button(_T("Apply Theme")) then
            if vehicle then
                local theme = vehicle_customizer.THEME_PACKS[state.theme_index]
                if theme and theme.value then
                    vehicle_customizer.apply_theme_pack(vehicle, theme)
                    ui.notify(_T("Theme applied") .. ": " .. theme.name, "Success")
                elseif theme and theme.name == "None" then
                    ui.notify(_T("Please select a theme"), "Warning")
                end
            else
                notify_no_vehicle()
            end
        end
    end)
end

-- -------------------------------------------------------------------------
-- UI: Quick Actions
-- -------------------------------------------------------------------------

local function draw_quick_actions(vehicle)
    ui.Group(_T("Quick Actions"), function()
        ui.Row({
            function()
                if ui.Button(_T("Max Performance")) then
                    if vehicle then
                        vehicle_customizer.apply_max_performance(vehicle)
                        ui.notify(_T("Max performance applied"), "Success")
                    else
                        notify_no_vehicle()
                    end
                end
            end,
            function()
                if ui.Button(_T("Max Visual")) then
                    if vehicle then
                        vehicle_customizer.apply_max_visual_upgrades(vehicle)
                        ui.notify(_T("Max visual upgrades applied"), "Success")
                    else
                        notify_no_vehicle()
                    end
                end
            end,
            function()
                if ui.Button(_T("Repair Vehicle")) then
                    if vehicle then
                        if N.SET_VEHICLE_FIXED then
                            pcall(N.SET_VEHICLE_FIXED, vehicle)
                        end
                        vehicle_customizer.clean_vehicle(vehicle)
                        ui.notify(_T("Vehicle repaired"), "Success")
                    else
                        notify_no_vehicle()
                    end
                end
            end,
        })

        ui.Row({
            function()
                if ui.Button(_T("Clean Vehicle")) then
                    if vehicle then
                        vehicle_customizer.clean_vehicle(vehicle)
                        ui.notify(_T("Vehicle cleaned"), "Success")
                    else
                        notify_no_vehicle()
                    end
                end
            end,
            function()
                if ui.Button(_T("Bulletproof Tires")) then
                    if vehicle then
                        vehicle_customizer.set_bulletproof_tires(vehicle, true)
                        ui.notify(_T("Bulletproof tires enabled"), "Success")
                    else
                        notify_no_vehicle()
                    end
                end
            end,
            function()
                if ui.Button(_T("Vehicle Invincible")) then
                    if vehicle then
                        vehicle_customizer.set_invincible(vehicle, true)
                        ui.notify(_T("Vehicle invincible enabled"), "Success")
                    else
                        notify_no_vehicle()
                    end
                end
            end,
        })
    end)
end

-- -------------------------------------------------------------------------
-- UI: Customization Tab
-- -------------------------------------------------------------------------

local function draw_customization_tab()
    if not vehicle_customizer then
        ui.Text(_T("VehicleCustomizer not loaded"))
        return
    end

    local vehicle = get_current_vehicle()

    ui.DualColumn(
        function()
            draw_color_section(vehicle)
            draw_lights_section(vehicle)
            draw_wheels_section(vehicle)
        end,
        function()
            draw_quick_actions(vehicle)
            draw_plate_section(vehicle)
            draw_theme_section(vehicle)
            draw_window_section(vehicle)
            draw_interior_section(vehicle)
        end
    )

    ui.DualColumn(
        function()
            draw_visual_mod_category(vehicle, VISUAL_MOD_CATEGORIES[1])
            draw_visual_mod_category(vehicle, VISUAL_MOD_CATEGORIES[3])
        end,
        function()
            draw_visual_mod_category(vehicle, VISUAL_MOD_CATEGORIES[2])
            draw_visual_mod_category(vehicle, VISUAL_MOD_CATEGORIES[4])
        end
    )
end

-- -------------------------------------------------------------------------
-- UI: Doors Section
-- -------------------------------------------------------------------------

local function draw_doors_section(vehicle)
    ui.Group(_T("Door Control"), function()
        ui.Row({
            function()
                local current = state.doors_open[0] or false
                local new_val = ui.Checkbox(_T("Front Left"), current)
                if new_val ~= current then
                    state.doors_open[0] = new_val
                    if vehicle then
                        vehicle_customizer.set_door_open(vehicle, 0, new_val)
                    end
                end
            end,
            function()
                local current = state.doors_open[1] or false
                local new_val = ui.Checkbox(_T("Front Right"), current)
                if new_val ~= current then
                    state.doors_open[1] = new_val
                    if vehicle then
                        vehicle_customizer.set_door_open(vehicle, 1, new_val)
                    end
                end
            end,
        })

        ui.Row({
            function()
                local current = state.doors_open[2] or false
                local new_val = ui.Checkbox(_T("Rear Left"), current)
                if new_val ~= current then
                    state.doors_open[2] = new_val
                    if vehicle then
                        vehicle_customizer.set_door_open(vehicle, 2, new_val)
                    end
                end
            end,
            function()
                local current = state.doors_open[3] or false
                local new_val = ui.Checkbox(_T("Rear Right"), current)
                if new_val ~= current then
                    state.doors_open[3] = new_val
                    if vehicle then
                        vehicle_customizer.set_door_open(vehicle, 3, new_val)
                    end
                end
            end,
        })

        ui.Row({
            function()
                local current = state.doors_open[4] or false
                local new_val = ui.Checkbox(_T("Hood"), current)
                if new_val ~= current then
                    state.doors_open[4] = new_val
                    if vehicle then
                        vehicle_customizer.set_door_open(vehicle, 4, new_val)
                    end
                end
            end,
            function()
                local current = state.doors_open[5] or false
                local new_val = ui.Checkbox(_T("Trunk"), current)
                if new_val ~= current then
                    state.doors_open[5] = new_val
                    if vehicle then
                        vehicle_customizer.set_door_open(vehicle, 5, new_val)
                    end
                end
            end,
        })

        ui.Row({
            function()
                if ui.Button(_T("Open All")) then
                    if vehicle then
                        vehicle_customizer.open_all_doors(vehicle)
                        for i = 0, 5 do
                            state.doors_open[i] = true
                        end
                        ui.notify(_T("All doors opened"), "Success")
                    else
                        notify_no_vehicle()
                    end
                end
            end,
            function()
                if ui.Button(_T("Close All")) then
                    if vehicle then
                        vehicle_customizer.close_all_doors(vehicle)
                        for i = 0, 5 do
                            state.doors_open[i] = false
                        end
                        ui.notify(_T("All doors closed"), "Success")
                    else
                        notify_no_vehicle()
                    end
                end
            end,
        })
    end)
end

-- -------------------------------------------------------------------------
-- UI: Windows Section
-- -------------------------------------------------------------------------

local function draw_windows_section(vehicle)
    ui.Group(_T("Window Control"), function()
        ui.Row({
            function()
                local current = state.windows_down[0] or false
                local new_val = ui.Checkbox(_T("Front Left"), current)
                if new_val ~= current then
                    state.windows_down[0] = new_val
                    if vehicle then
                        vehicle_customizer.set_window_down(vehicle, 0, new_val)
                    end
                end
            end,
            function()
                local current = state.windows_down[1] or false
                local new_val = ui.Checkbox(_T("Front Right"), current)
                if new_val ~= current then
                    state.windows_down[1] = new_val
                    if vehicle then
                        vehicle_customizer.set_window_down(vehicle, 1, new_val)
                    end
                end
            end,
        })

        ui.Row({
            function()
                local current = state.windows_down[2] or false
                local new_val = ui.Checkbox(_T("Rear Left"), current)
                if new_val ~= current then
                    state.windows_down[2] = new_val
                    if vehicle then
                        vehicle_customizer.set_window_down(vehicle, 2, new_val)
                    end
                end
            end,
            function()
                local current = state.windows_down[3] or false
                local new_val = ui.Checkbox(_T("Rear Right"), current)
                if new_val ~= current then
                    state.windows_down[3] = new_val
                    if vehicle then
                        vehicle_customizer.set_window_down(vehicle, 3, new_val)
                    end
                end
            end,
        })

        ui.Row({
            function()
                if ui.Button(_T("Roll Down All")) then
                    if vehicle then
                        vehicle_customizer.roll_down_all_windows(vehicle)
                        for i = 0, 3 do
                            state.windows_down[i] = true
                        end
                        ui.notify(_T("All windows rolled down"), "Success")
                    else
                        notify_no_vehicle()
                    end
                end
            end,
            function()
                if ui.Button(_T("Roll Up All")) then
                    if vehicle then
                        vehicle_customizer.roll_up_all_windows(vehicle)
                        for i = 0, 3 do
                            state.windows_down[i] = false
                        end
                        ui.notify(_T("All windows rolled up"), "Success")
                    else
                        notify_no_vehicle()
                    end
                end
            end,
        })

        ui.Row({
            function()
                if ui.Button(_T("Smash Windows")) then
                    if vehicle then
                        for i = 0, 3 do
                            vehicle_customizer.smash_window(vehicle, i)
                        end
                        ui.notify(_T("All windows smashed"), "Info")
                    else
                        notify_no_vehicle()
                    end
                end
            end,
            function()
                if ui.Button(_T("Repair Windows")) then
                    if vehicle then
                        for i = 0, 3 do
                            vehicle_customizer.fix_window(vehicle, i)
                        end
                        ui.notify(_T("All windows repaired"), "Success")
                    else
                        notify_no_vehicle()
                    end
                end
            end,
        })
    end)
end

-- -------------------------------------------------------------------------
-- UI: Convertible Section
-- -------------------------------------------------------------------------

local function draw_convertible_section(vehicle)
    local is_convertible = vehicle and vehicle_customizer.is_convertible(vehicle)

    -- Only show if vehicle is present and supports convertible
    if not is_convertible then
        return
    end

    ui.Group(_T("Convertible Control"), function()

        local new_roof = ui.Checkbox(_T("Roof Closed"), state.convertible_roof)
        if new_roof ~= state.convertible_roof then
            state.convertible_roof = new_roof
            if vehicle then
                vehicle_customizer.set_convertible_roof(vehicle, new_roof)
            end
        end

        if ui.Button(_T("Open Roof")) then
            if vehicle then
                vehicle_customizer.set_convertible_roof(vehicle, false)
                state.convertible_roof = false
                ui.notify(_T("Convertible roof opened"), "Success")
            end
        end

        if ui.Button(_T("Close Roof")) then
            if vehicle then
                vehicle_customizer.set_convertible_roof(vehicle, true)
                state.convertible_roof = true
                ui.notify(_T("Convertible roof closed"), "Success")
            end
        end
    end)
end

-- -------------------------------------------------------------------------
-- UI: Engine & Lights Section
-- -------------------------------------------------------------------------

local function draw_engine_lights_section(vehicle)
    ui.Group(_T("Engine & Lights"), function()
        ui.Row({
            function()
                local new_lights = ui.Checkbox(_T("Force Lights On"), state.lights_on)
                if new_lights ~= state.lights_on then
                    state.lights_on = new_lights
                    if vehicle then
                        vehicle_customizer.set_vehicle_lights(vehicle, new_lights and 2 or 0)
                    end
                end
            end,
            function()
                local new_hazard = ui.Checkbox(_T("Hazard Lights"), state.hazard_lights)
                if new_hazard ~= state.hazard_lights then
                    state.hazard_lights = new_hazard
                    if vehicle then
                        vehicle_customizer.set_hazard_lights(vehicle, new_hazard)
                    end
                end
            end,
        })

        ui.Row({
            function()
                if ui.Button(_T("Left Turn Signal")) then
                    if vehicle then
                        vehicle_customizer.set_left_indicator(vehicle, true)
                        vehicle_customizer.set_right_indicator(vehicle, false)
                        ui.notify(_T("Left turn signal on"), "Info")
                    else
                        notify_no_vehicle()
                    end
                end
            end,
            function()
                if ui.Button(_T("Right Turn Signal")) then
                    if vehicle then
                        vehicle_customizer.set_left_indicator(vehicle, false)
                        vehicle_customizer.set_right_indicator(vehicle, true)
                        ui.notify(_T("Right turn signal on"), "Info")
                    else
                        notify_no_vehicle()
                    end
                end
            end,
        })

        if ui.Button(_T("Turn Off Signals")) then
            if vehicle then
                vehicle_customizer.set_left_indicator(vehicle, false)
                vehicle_customizer.set_right_indicator(vehicle, false)
                ui.notify(_T("Turn signals off"), "Info")
            else
                notify_no_vehicle()
            end
        end

        local new_engine = ui.Checkbox(_T("Engine On"), state.engine_on)
        if new_engine ~= state.engine_on then
            state.engine_on = new_engine
            if vehicle then
                vehicle_customizer.set_engine_on(vehicle, new_engine)
            end
        end
    end)
end

-- -------------------------------------------------------------------------
-- UI: Extras Section
-- -------------------------------------------------------------------------

local function draw_extras_section(vehicle)
    ui.Group(_T("Vehicle Extras"), function()
        if not vehicle then
            ui.Text(_T("Enter vehicle to show available extras"))
            return
        end

        local has_extras = false

        for i = 1, 15 do
            if vehicle_customizer.does_extra_exist(vehicle, i) then
                has_extras = true
                local current = state.extras[i]
                if current == nil then
                    current = vehicle_customizer.is_extra_enabled(vehicle, i)
                    state.extras[i] = current
                end

                local new_val = ui.Checkbox(_T("Extra") .. " " .. i, current)
                if new_val ~= current then
                    state.extras[i] = new_val
                    vehicle_customizer.set_extra(vehicle, i, new_val)
                end
            end
        end

        if not has_extras then
            ui.Text(_T("Current vehicle has no extras"))
        end
    end)
end

-- -------------------------------------------------------------------------
-- UI: Control Tab
-- -------------------------------------------------------------------------

local function draw_control_tab()
    if not vehicle_customizer then
        ui.Text(_T("VehicleCustomizer not loaded"))
        return
    end

    local vehicle = get_current_vehicle()

    ui.DualColumn(
        function()
            draw_doors_section(vehicle)
            draw_windows_section(vehicle)
        end,
        function()
            draw_engine_lights_section(vehicle)
            ui.Row({
                function()
                    draw_convertible_section(vehicle)
                end,
                function()
                    draw_extras_section(vehicle)
                end,
            })
        end
    )
end

-- -------------------------------------------------------------------------
-- Main Draw
-- -------------------------------------------------------------------------

function module.draw()
    state.main_tab = ui.SubTabBar(MAIN_TABS, state.main_tab)

    if state.main_tab == 1 then
        draw_customization_tab()
    elseif state.main_tab == 2 then
        draw_control_tab()
    end
end

function module.on_tick()
end

return module
